package jzqb.controller.admin;

import haha.life.common.CutPageBean;
import haha.life.common.PermissionTypeTool;
import haha.life.common.SysTool;
import haha.life.normal.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jzqb.annotation.PermissionCheck.PermissionCheck;
import jzqb.bo.AdminPermissionGroupBo;
import jzqb.bo.AdminPermissionGroup_getuserBo;
import jzqb.common.LogResult;
import jzqb.common.Result;
import jzqb.po.AdminPermissionGroupPo;
import jzqb.po.AdminUserPo;
import jzqb.service.AdminCheckPermissionsService;
import jzqb.service.AdminPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

@RestController
@CrossOrigin
@RequestMapping("/adminPermission")
@Api(tags = "权限相关接口")
public class AdminPermissionController {
    @Autowired
    private AdminPermissionService adminPermissionService;

    @Autowired
    private AdminCheckPermissionsService adminCheckPermissionsService;

    @PermissionCheck(permissionTypeList = {PermissionTypeTool.sys_mgt})
    @ApiOperation(value = "查询权限组")
    @GetMapping("PermissionGrouplist")
    public Result PermissionGrouplist(
            @PathParam("permissionGroupName") String permissionGroupName,
            @PathParam("orderType") String orderType,
            @PathParam("orderValue") String orderValue,
            @PathParam("page") int page,
            @PathParam("pageSize") int pageSize
    ) {

        AdminPermissionGroupBo adminPermissionGroupBo = new AdminPermissionGroupBo();
        adminPermissionGroupBo.setPermissionGroupName(permissionGroupName);
        adminPermissionGroupBo.setOrderType(orderType);
        adminPermissionGroupBo.setOrderValue(orderValue);
        adminPermissionGroupBo.setPage(page);
        adminPermissionGroupBo.setPageSize(pageSize);
        adminPermissionGroupBo.setStartPage((page - 1) * pageSize);
        CutPageBean<AdminPermissionGroupPo> cutPageBean = adminPermissionService.cutPage(adminPermissionGroupBo);
        return Result.succ(cutPageBean);
    }

    @PermissionCheck(permissionTypeList = {PermissionTypeTool.sys_mgt})
    @ApiOperation(value = "通过'用户名或id'查用户")
    @GetMapping("getuser")
    public Result getuser(
            @PathParam("user") String user,
            @PathParam("page") int page,
            @PathParam("limit") int limit,
            @PathParam("ids") String ids,
            @PathParam("switchOpen") String switchOpen
    ) {
        //在switch状态下查询
        if("true".equals(switchOpen)){
            AdminPermissionGroup_getuserBo adminUserInfoBo = new AdminPermissionGroup_getuserBo();
            adminUserInfoBo.setUsernameORid(user);
            adminUserInfoBo.setIds(ids);
            adminUserInfoBo.setOrderType("姓名");
            adminUserInfoBo.setOrderValue("asc");
            adminUserInfoBo.setPage(page);
            adminUserInfoBo.setPageSize(limit);
            adminUserInfoBo.setStartPage((page - 1) * limit);
            CutPageBean<AdminUserPo> cutPageBean = adminPermissionService.cutPage_switchShow(adminUserInfoBo);
            return LogResult.succ(cutPageBean.getList(), cutPageBean.getCount());
        }else{
            AdminPermissionGroup_getuserBo adminUserInfoBo = new AdminPermissionGroup_getuserBo();
            adminUserInfoBo.setUsernameORid(user == null ? "" : user);
            adminUserInfoBo.setOrderType("姓名");
            adminUserInfoBo.setOrderValue("asc");
            adminUserInfoBo.setPage(page);
            adminUserInfoBo.setPageSize(limit);
            adminUserInfoBo.setStartPage((page - 1) * limit);
            CutPageBean<AdminUserPo> cutPageBean = adminPermissionService.cutPage_(adminUserInfoBo);
            return LogResult.succ(cutPageBean.getList(), cutPageBean.getCount());
        }
    }

    @PermissionCheck(permissionTypeList = {PermissionTypeTool.sys_mgt})
    @ApiOperation(value = "新建/编辑权限组")
    @PostMapping("addeditPermissionGroup")
    public Result addeditPermissionGroup(
//            @RequestBody AdminPermissionGroupPo adminPermissionGroupPo,
            HttpServletRequest request,
            @RequestBody HashMap<String, String> map
    ) {
        String id = map.get("id");
        String name = map.get("name");
        String departmentMemberID = map.get("departmentMemberID");
        String userMemberID = map.get("userMemberID");
        String dataPermission = map.get("newDataPermission");

        AdminPermissionGroupPo adminPermissionGroupPo = new AdminPermissionGroupPo();
        adminPermissionGroupPo.setPermissionGroupID(id);
        adminPermissionGroupPo.setPermissionGroupName(name);
        adminPermissionGroupPo.setDepartments(departmentMemberID);
        adminPermissionGroupPo.setUsers(userMemberID);
        adminPermissionGroupPo.setNewDataPermission(dataPermission);

        String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());


        adminPermissionGroupPo.setCreteTime(now);
        adminPermissionGroupPo.setCreateor(TokenUtils.getCurrentUserId(request));
        if (adminPermissionService.addeditPermissionGroup(adminPermissionGroupPo)) {
            return Result.succ("成功!");
        } else {
            return Result.succ("失败!");
        }

    }


    @PermissionCheck(permissionTypeList = {PermissionTypeTool.sys_mgt})
    @ApiOperation(value = "删除权限组")
    @PostMapping("deletePermissionGroup")
    public Result deletePermissionGroup(
            @RequestBody AdminPermissionGroupPo adminPermissionGroupPo
    ) {
        if (adminPermissionService.deletePermissionGroup(adminPermissionGroupPo)) {
            return Result.succ("成功!");
        } else {
            return Result.succ("失败!");
        }
    }


    @PermissionCheck(permissionTypeList = {PermissionTypeTool.sys_mgt})
    @ApiOperation(value = "查询某个权限组已有的权限")
    @GetMapping("getPermissionTreeExisted")
    public Result getPermissionTreeExisted(@PathParam("permissionGroupID") String permissionGroupID) {
        return Result.succ(adminPermissionService.getPermissionTreeExisted(permissionGroupID));
    }

    @GetMapping("showSelfBaseButton")
    public Result showSelfBaseButton(
            HttpServletRequest request
    ) {
//        String[] permissionTypeList = new String[]{SysTool.QBFW_SelfBaseButton};
//        Boolean res = adminCheckPermissionsService.inPermissionGroup(request, permissionTypeList);
        String userid = TokenUtils.getCurrentUserId(request);
        Boolean res = adminCheckPermissionsService.showSelfBaseButton(userid);
        return Result.succ(res);
    }

    /**
     * 当前用户是否具有 情报服务- 退审权限-->业务管理员
     *
     * @param request
     * @return
     */
    @GetMapping("/showTuiShenButton")
    public Result showTuiShenButton(
            HttpServletRequest request
    ) {
        String[] permissionTypeList = new String[]{PermissionTypeTool.front_businessManageGroupId};
        Boolean res = adminCheckPermissionsService.inPermissionGroup(request, permissionTypeList);
        return Result.succ(res);
    }

    /**
     * 当前用户是否具有 情报百科- 审批权限(是否展示情报百科-审核按钮)-->业务管理员
     *
     * @param request
     * @return
     */
    @GetMapping("/showShenHeButton")
    public Result showShenHeButton(
            HttpServletRequest request
    ) {
        String[] permissionTypeList = new String[]{PermissionTypeTool.front_businessManageGroupId};
        Boolean res = adminCheckPermissionsService.inPermissionGroup(request, permissionTypeList);
        return Result.succ(res);
    }

    /**
     * 当前用户是否具有 情报服务- 审批权限-->业务管理员,系统管理员,部门领导
     *
     * @param request
     * @return
     */
    @GetMapping("/showQBFWShenHeButton")
    public Result showQBFWShenHeButton(
            HttpServletRequest request
    ) {
        String[] permissionTypeList = new String[]{PermissionTypeTool.front_businessManageGroupId, PermissionTypeTool.front_SysManageGroupId};
        //业务权限管理组和系统管理组 满足任意一个为true
        Boolean PermissionGroup = adminCheckPermissionsService.inPermissionGroup(request, permissionTypeList);
       //是任意一个部门领导为true
        Boolean isDeptLeader = adminCheckPermissionsService.isDeptLeader(request);
        Boolean res = PermissionGroup || isDeptLeader;
        return Result.succ(res);
    }

    /**
     * 是否展示情报百科-富文本模板制定按钮
     *
     * @param request
     * @return
     */
    @GetMapping("/showQBBKTemplateButton")
    public Result showQBBKTemplateButton(
            HttpServletRequest request
    ) {
        String[] permissionTypeList = new String[]{PermissionTypeTool.front_businessManageGroupId};
        Boolean res = adminCheckPermissionsService.inPermissionGroup(request, permissionTypeList);
        return Result.succ(res);
    }


}